package com.idbk.chargestation.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.idbk.chargestation.R;
import com.idbk.chargestation.bean.JsonGunInfor;
import com.idbk.chargestation.bean.JsonPointPile;

import java.util.List;

/**
 * 多枪界面适配器
 * Created by wuz on 2019/1/12.
 */

public class GunGridViewAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private int gunNum = 0; //枪的数量
    private int gunChoose = 0; //已连接枪数
    private List<JsonGunInfor> mData;
    private int location =  100;
    private String mSpeed;


    public GunGridViewAdapter(Context context, List<JsonGunInfor> data, String speed){
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mData = data;
        mSpeed = speed;

    }

    private class Holder {
        private ImageView mImageStatus;
        private ImageView mImageCheck;
        private TextView mTextGunNum;
        private TextView mTextGunRate;
        private TextView mTextGunStatus;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v;
        Holder hold;
        gunChoose = 0;
        gunNum = mData.size();
        for (int i = 0; i < gunNum; i++){
            if (mData.get(i).gunStatus ==  JsonPointPile.STATUS_WAIT){
                gunChoose++;
            }
        }

        if (convertView == null) {
            v = mInflater.inflate(R.layout.item_grid_gun, parent, false);
            hold = new Holder();

            hold.mImageStatus = (ImageView) v.findViewById(R.id.imageView_Status);
            hold.mTextGunNum = (TextView) v.findViewById(R.id.textview_gun_name);
            hold.mTextGunRate = (TextView) v.findViewById(R.id.textview_gun_rate);
            hold.mTextGunStatus = (TextView) v.findViewById(R.id.textview_gun_status);
            hold.mImageCheck = (ImageView) v.findViewById(R.id.check);
            v.setTag(hold);
        } else {
            v = convertView;
            hold = (Holder) v.getTag();
        }

        if (gunChoose >= 2 && mData.get(position).gunStatus == JsonPointPile.STATUS_WAIT ){
            hold.mImageCheck.setVisibility(View.VISIBLE);
        }

        hold.mTextGunNum.setText("枪"+mData.get(position).gunNum);
        hold.mTextGunRate.setText(mSpeed);
        String fast = "快充";
        String superFast = "超充";
        if (mSpeed.equals(fast) || mSpeed.equals(superFast)) {
            hold.mTextGunRate.setBackgroundResource(R.drawable.textview_style_blue);
        }

        if (mData.get(position).gunStatus == JsonPointPile.STATUS_CHARGING || mData.get(position).gunStatus == JsonPointPile.STATUS_FINISH){
            hold.mImageStatus.setImageResource(R.drawable.ico_charging_gun_disabled_small);
            hold.mTextGunStatus.setText("被占用");
            hold.mTextGunStatus.setTextColor((mContext.getResources().getColor(R.color.tj_text_content_9)));
        }else if (mData.get(position).gunStatus == JsonPointPile.STATUS_WAIT){
            hold.mImageStatus.setImageResource(R.drawable.ico_charging_gun_connected_small);
            hold.mTextGunStatus.setText("已连接");
            hold.mTextGunStatus.setTextColor((mContext.getResources().getColor(R.color.tj_text_content_d)));
            if (location == position){
                hold.mImageCheck.setImageResource(R.drawable.btn_checkbox_active);
            }else {
                hold.mImageCheck.setImageResource(R.drawable.btn_checkbox_normal);
            }
        }else if (mData.get(position).gunStatus == JsonPointPile.STATUS_FAULT){
            hold.mImageStatus.setImageResource(R.drawable.ico_charging_gun_disabled_small);
            hold.mTextGunStatus.setText("故障");
            hold.mTextGunStatus.setTextColor((mContext.getResources().getColor(R.color.tj_text_content_9)));
        }else if (mData.get(position).gunStatus == JsonPointPile.STATUS_WARN){
            hold.mImageStatus.setImageResource(R.drawable.ico_charging_gun_disabled_small);
            hold.mTextGunStatus.setText("告警");
            hold.mTextGunStatus.setTextColor((mContext.getResources().getColor(R.color.tj_text_content_9)));
        }

        return v;
    }

    public void setSeclection(int position) {
        location = position;
    }
}
